unit hadeuibconnection;

{$mode objfpc}{$H+}

interface

uses
  Classes,
  SysUtils,
  hadebaseconnection,
  uib;
Type

  { THadeUIBConnection }

  THadeUIBConnection = class(THadeBaseConnection)
  protected
    FConnection : TUIBDatabase;
    FTransaction : TUIBTransaction;

    function getParams: TStrings;
    procedure setParams(AValue: TStrings);
  public
    constructor Create;
    destructor Destroy; override;

    procedure Connect;
    procedure Disconnect;

    Procedure setDataSet(Const ADataSet:Pointer);

    procedure ExecuteDirect(const ASql: string);
    function IsConnected: boolean;

    procedure StartTransaction;
    procedure Commit;
    procedure Rollback;
  end;

implementation
uses
  uiblib,
  hadeexception;
{ THadeUIBConnection }

function THadeUIBConnection.getParams: TStrings;
begin
  Result := FConnection.Params;
end;

procedure THadeUIBConnection.setParams(AValue: TStrings);
begin
  FConnection.Params:=AValue;
end;

constructor THadeUIBConnection.Create;
begin
  FConnection:=TUIBDatabase.Create(nil);
  fTransaction:=TUIBTransaction.Create(nil);
  FTransaction.DataBase:=FConnection;
end;

destructor THadeUIBConnection.Destroy;
begin
  disconnect;
  FreeAndNil(FConnection);
  FreeAndNil(FTransaction);
  inherited Destroy;
end;

procedure THadeUIBConnection.Connect;
begin
  FConnection.DatabaseName := Host+':'+Database;
  FConnection.UserName := Self.Username;
  FConnection.Password := Self.Password;

  if not self.IsConnected then
  begin
    try
      FConnection.Connected := True;
    except
      on E: EUIBError do
        raise EHadeDatabase.Create(E.Message);
    end;
  end;
end;

procedure THadeUIBConnection.Disconnect;
begin
  if self.IsConnected then
    FConnection.Connected := False;
end;

procedure THadeUIBConnection.setDataSet(const ADataSet: Pointer);
begin
  TUIBQuery(ADataSet).DataBase:=FConnection;
end;

procedure THadeUIBConnection.ExecuteDirect(const ASql: string);
begin

end;

function THadeUIBConnection.IsConnected: boolean;
begin
  Result := FConnection.Connected;
end;

procedure THadeUIBConnection.StartTransaction;
begin
  self.Connect;
  Try
    FTransaction.StartTransaction;
  except
    on E: EUIBError do
      raise EHadeDatabase.Create(E.Message);
  end;
end;

procedure THadeUIBConnection.Commit;
begin
  try
    FTransaction.Commit;
  except
    on E: EUIBError do
      raise EHadeDatabase.Create(E.Message);
  end;
end;

procedure THadeUIBConnection.Rollback;
begin
  try
    FTransaction.Rollback;
  except
    on E: EUIBError do
      raise EHadeDatabase.Create(E.Message);
  end;
end;

end.

